package com.niji.littly_donkey_android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.makeramen.roundedimageview.RoundedImageView;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.activity.GetPhoneCodeActivity;
import com.niji.littly_donkey_android.activity.HomePageActivity;
import com.niji.littly_donkey_android.bean.Feedback;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.*;
import com.niji.littly_donkey_android.view.CircleImageView;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FeedbackAdapter extends ArrayAdapter<Feedback> {


    private static final String TAG = "FeedbackAdapter";

    private Context context;

    private Activity activity;

    private ArrayList<Feedback> feedbacks;

    public FeedbackAdapter(Context context, ArrayList<Feedback> members)
    {
        super(context,0, members);
        this.context = context;
        this.activity = (Activity) context;
        this.feedbacks = members;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Feedback feedback = getItem(position);

        if(convertView == null)
        {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_feedback, parent, false);
        }

        CircleImageView roundedImageView = convertView.findViewById(R.id.feedback_head);
        TextView feedbackName = convertView.findViewById(R.id.feedback_name);
        TextView feedbackMessage = convertView.findViewById(R.id.feedback_message);
        TextView feedbacId = convertView.findViewById(R.id.feedback_id);
        Button feedbackBtn = convertView.findViewById(R.id.feedback_btn);
        //thumb.setImageBitmap(BitMap.getInstance().returnBitMap(u.getThumb()));
        //android4.0后要访问网络资源需要单开线程，加入这段就不会报错了
        NetworkGlideImageLoader.getInstance().displayImage((Activity) context,feedback.getHead(),roundedImageView,0,0);
        feedbackName.setText(feedback.getName());
        feedbackMessage.setText(feedback.getMessage());
        feedbacId.setText("" + feedback.getId());
        feedbackBtn.setText(feedback.getBtn());
        if ("拉黑".equals(feedback.getBtn())){
            feedbackBtn.setBackground(convertView.getResources().getDrawable(R.drawable.black_push));
            feedbackBtn.setTextColor(Color.parseColor("#ffffff"));
        }else {
            feedbackBtn.setBackground(convertView.getResources().getDrawable(R.drawable.black_pull));
            feedbackBtn.setTextColor(Color.parseColor("#f93251"));
        }

        feedbackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = UrlUtil.URL + "/user/black/list/del";
                RequestQueue requestQueue= Volley.newRequestQueue(context);
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        try {
                            Map<String, Object> map = JsonUtil.jsonToMap(s);
                            Integer code = (Integer) map.get("code");
                            if (code == 0) {
                                Toast.makeText(context, "移除黑名单成功", Toast.LENGTH_SHORT).show();
                                feedbackBtn.setText("拉黑");
                                feedbacks.remove(position);
                                notifyDataSetChanged();
                            } else if(code == -1){
                                Toast.makeText(context, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(context, GetPhoneCodeActivity.class);
                                intent.putExtra("loginCode","-1");
                                activity.startActivity(intent);
                                activity.finish();
                            }else {
                                Toast.makeText(context, "不存在这条拉黑信息", Toast.LENGTH_SHORT).show();
                                activity.overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                                activity.finish();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, volleyError.getMessage(), volleyError);
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        HashMap<String, String> map = new HashMap<>();
                        String user_id = feedbacId.getText().toString().trim();
                        map.put("black_id",user_id);
                        return map;
                    }
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        String sesseionid = new ConnectViaSession(context).GetSession();
                        if (!sesseionid.equals("")) {
                            HashMap<String, String> headers = new HashMap<String, String>();
                            headers.put("cookie", sesseionid);
                            return headers;
                        } else {
                            return super.getHeaders();
                        }
                    }
                };

                requestQueue.add(stringRequest);
            }
        });
        return convertView;
    }

}



